/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cursos.bd;

import conexao.ConexaoSingleton;
import cursos.classes.Cursos;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Gabi
 */
public class CursosDAO {

    private EntityManager manager;
    private Query query;

    public CursosDAO() throws Exception {
        manager = ConexaoSingleton.getConexao();
    }

    public Cursos getCursos(Integer codigo) {
        Cursos curso = manager.find(Cursos.class, codigo);
        return curso;

    }

    public List<Cursos> getCursos() {
        query = manager.createQuery("select c from Cursos c order by nome");
        return query.getResultList();
    }

    public void salvar(Cursos curso) {
        manager.getTransaction().begin();
        manager.persist(curso);
        manager.getTransaction().commit();
    }

    public void excluir(Cursos curso) {
        manager.getTransaction().begin();
        manager.remove(curso);
        manager.getTransaction().commit();
    }

    public List<Cursos> pesquisaCurso(String curso) {
        query = manager.createNamedQuery("cursoPorNome");
        query.setParameter("nomeCurso", "%" + curso + "%");
        return query.getResultList();
    }
}
